#!/alidata/server/php/bin/php
<?php
error_reporting(0);
ini_set('memory_limit', '1000M');
set_time_limit(0);

/**
 * 根据source_url判重
 */

// type字典
// 当前数据
$currList = array();
// 上一个数据
$lastList = array();
// 重复数据数组
$repeatList = array();

/** 
 *  reducer_1.php
 *      input:根据source_url排序的gov表数据，格式source_url\tTYPE\tjson(所有数据数组的格式)
 *      handle:异常数据插入house_sell_gov表，is_contrast=8
 *      output:
 * @created 2016-01-16
 * @updated 2016-03-14
 * @updated 2016-05-03 修改is_contrast逻辑(等于7,独立房源)，并将第一轮筛选出来的房源入库到sell&info表
 * @by lily
 */
CONST is_con = 7;
CONST cntt=1;
CONST devi=0;
CONST weig=0;
$unique_id=0;
while (($line_json = fgets(STDIN)) !== false) {   //line_json=>source_url\tjson格式
    
    $line_json = trim($line_json);
    if (empty($line_json))
        continue;
    list ($source_url, $type, $list_json) = explode("\t", $line_json);
    $type = trim($type);
    // 有问题的数据直接输出,is_contrast=8
    if (intval($type) != 1) {
		$one_dejson=json_decode($list_json,true);
		//插入gov表
		 echo "INSERT INTO house_sell_gov(id,broker_id,house_price,house_desc,house_title,cityarea_id,
		cityarea2_id,house_floor,
		house_topfloor,house_toward,house_room,house_hall,house_toilet,house_kitchen,house_fitment,
		house_feature,house_built_year,use_area,owner_name,owner_phone,service_phone,house_support,
		created,
		updated,status,source,source_owner,app_url,wap_url,source_url,is_checked,click_num,refresh,
		house_type,house_totalarea,borough_id,borough_name,house_pic_unit,house_pic_layout,house_number,
		is_fill,is_contrast,public_time,source_name) VALUES";
		echo " ('".$one_dejson['id']."','"
		.$one_dejson['broker_id']."','".$one_dejson['house_price']."','".
		str_replace("'","",str_replace("\\","",$one_dejson['house_desc']))."','".
		str_replace("'","",str_replace("\\","",$one_dejson['house_title']))."','".
		$one_dejson['cityarea_id']."','".$one_dejson['cityarea2_id']."','".
		$one_dejson['house_floor']."','".$one_dejson['house_topfloor']."','".
		$one_dejson['house_toward']."','".$one_dejson['house_room']."','".
		$one_dejson['house_hall']."','".$one_dejson['house_toilet']."','".
		$one_dejson['house_kitchen']."','".$one_dejson['house_fitment']."','".
		str_replace("'","",$one_dejson['house_feature'])."','".
		str_replace("'","",$one_dejson['house_built_year'])."','".
		$one_dejson['use_area']."','".
		str_replace("'","",$one_dejson['owner_name'])."','".
		$one_dejson['owner_phone']."','".
		$one_dejson['service_phone']."','".$one_dejson['house_support']."','".
		$one_dejson['created']."','".$one_dejson['updated']."','".
		$one_dejson['status']."','".$one_dejson['source']."','".
		$one_dejson['source_owner']."','".$one_dejson['app_url']."','".
		$one_dejson['wap_url']."','".$one_dejson['source_url']."','".
		$one_dejson['is_checked']."','".$one_dejson['click_num']."','".
		$one_dejson['refresh']."','".$one_dejson['house_type']."','".
		$one_dejson['house_totalarea']."','".$one_dejson['borough_id']."','".
		$one_dejson['borough_name']."','".$one_dejson['house_pic_unit']."','".
		$one_dejson['house_pic_layout']."','".$one_dejson['house_number']."','".
		$one_dejson['is_fill']."','".is_con."','".
		$one_dejson['public_time']."','".$one_dejson['source_name']."');";
		
		//插入info表
		if($one_dejson=='10') $is_fangzhu=1;
		else $is_fangzhu=0;
		echo "INSERT INTO house_sell(id,borough_id,house_type,cityarea2_id,cityarea_id,house_room,house_hall,
		house_totalarea,borough_name,min_price,updated,cnt,is_fangzhu,house_title) VALUES";
		echo " ('".$unique_id."','".
		$one_dejson['borough_id']."','".$one_dejson['house_type']."','".
		$one_dejson['cityarea2_id']."','".$one_dejson['cityarea_id']."','".
		$one_dejson['house_room']."','".$one_dejson['house_hall']."','".
		$one_dejson['house_totalarea']."','".$one_dejson['borough_name']."','".
		$one_dejson['house_price']."','".$one_dejson['updated']."','".cntt."','".
		$is_fangzhu."','".
		str_replace("'","",str_replace("\\","",$one_dejson['house_title']))."');";
		
		//插入sell表
		echo "INSERT INTO house_sell_info(id,broker_id,house_price,house_desc,house_title,
	cityarea2_id,house_floor,house_topfloor,house_toward,house_toilet,house_kitchen,
	house_fitment,house_feature,house_built_year,use_area,owner_name,owner_phone,service_phone,
	house_support,created,updated,status,source,source_url,is_checked,click_num,
	refresh,deviation,gov_id,weight,source_name) VALUES";
		echo " ('".$unique_id."','"
			.$one_dejson['broker_id']."','".$one_dejson['house_price']."','".
			str_replace("'","",str_replace("\\","",$one_dejson['house_desc']))."','".
			str_replace("'","",str_replace("\\","",$one_dejson['house_title']))."','".
			$one_dejson['cityarea2_id']."','".$one_dejson['house_floor']."','".
			$one_dejson['house_topfloor']."','".$one_dejson['house_toward']."','".
			$one_dejson['house_toilet']."','".$one_dejson['house_kitchen']."','".
			$one_dejson['house_fitment']."','".
			str_replace("'","",$one_dejson['house_feature'])."','".
			str_replace("'","",$one_dejson['house_built_year'])."','".
			$one_dejson['use_area']."','".
			str_replace("'","",$one_dejson['owner_name'])."','".
			$one_dejson['owner_phone']."','".
			$one_dejson['service_phone']."','".$one_dejson['house_support']."','".
			$one_dejson['created']."','".$one_dejson['updated']."','".
			$one_dejson['status']."','".$one_dejson['source']."','".
			$one_dejson['source_url']."','".$one_dejson['is_checked']."','".
			$one_dejson['click_num']."','".$one_dejson['refresh']."','".
			devi."','".$one_dejson['id']."','".weig."','".$one_dejson['source_name']."');";
        $unique_id=$unique_id+1;
        continue;
    }
}

?>

